package best.driver.android.Activity.Order;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;
import best.driver.android.Activity.Base.BaseActivity;
import best.driver.android.Activity.Order.Adapter.OrderItemAdapter;
import best.driver.android.AppInfo;
import best.driver.android.Beans.OrderListInfo;
import best.driver.android.Beans.OrderListItemInfo;
import best.driver.android.BestApp;
import best.driver.android.R;
import best.driver.android.RequestFactory;
import best.driver.android.Utils.Logger;
import best.driver.android.View.BasePullToRefreshListView;
import best.driver.android.common.app.AppProxyFactory;
import best.driver.android.common.network.*;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import java.util.ArrayList;
import java.util.List;

public class OrderListActivity extends BaseActivity implements BasePullToRefreshListView.OnPullDownListener{

    private int mPage = 1;

    private final static int PAGE_SIZE = 10;

    boolean mHasMore = true;

    @InjectView(R.id.prv_order_list)
    BasePullToRefreshListView mPrvOrders;

    OrderItemAdapter mAdapter;

    List<OrderListInfo.Records> mItems = new ArrayList<OrderListInfo.Records>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_order_list);
        ButterKnife.inject(this, view);
        mPrvOrders.setOnPullDownListener(this);
        mAdapter = new OrderItemAdapter(this);
        mPrvOrders.setAdapter(mAdapter);
        mAdapter.setData(mItems);
        mPrvOrders.setRefreshEnabled(true);
        setTopTopic("我的订单");
        setLeftIvBak(R.drawable.icon_navibar_back);
        getOrders(mPage, PAGE_SIZE, true);
    }

    @Override
    public void onRefresh() {
        getOrders(1, PAGE_SIZE, false);
    }

    @Override
    public void onMore() {
        if(mHasMore) {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    getOrders(mPage, PAGE_SIZE, false);
                }
            }, 100);

        }
    }

    private void getOrders(final int page, int pageSize, boolean isFirst) {
        if(isFirst) {
            showWaiting();
        }
        ApiObjectRequest<OrderListInfo> request = RequestFactory.createGetOrderListInfo(String.valueOf(page), String.valueOf(pageSize), null);
        request.setListener(new ApiRequest.ApiRequestListener<OrderListInfo>() {
            @Override
            public void onRequestSuccess(OrderListInfo response) {
                hideWaiting();
                mPrvOrders.onRefreshComplete();
                if(response == null) {
                    mPrvOrders.hideAutoLoadMore();
                    mPrvOrders.onLoadMoreComplete();
                }
                if(response != null) {
                    if(mPage > page) {
                        mPage = page;
                        mItems.clear();
                    }
                    mPage++;
                    mItems.addAll(response.records);
                    mHasMore = !response.lastPageFlag;
                    if(mHasMore) {
                        mPrvOrders.showAutoLoadMore();
                        mPrvOrders.onLoadMoreComplete();
                    } else {
                        mPrvOrders.hideAutoLoadMore();
                        mPrvOrders.onLoadMoreComplete();
                    }
                    mAdapter.setData(mItems);
                }
            }

            @Override
            public void onRequestError(BestError error) {
                hideWaiting();
                mPrvOrders.hideAutoLoadMore();
                mPrvOrders.onLoadMoreComplete();
                mPrvOrders.onRefreshComplete();
            }
        });
        AppProxyFactory.getProxy().getNetworkManager().enqueueRequest(request);
    }

}
